package com.mall.service.authority.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.UmsResourceCategory;
import com.mall.service.authority.service.UmsResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/resourceCategory")
public class UmsResourceCategoryController {

    @Autowired
    private UmsResourceCategoryService umsResourceCategoryService;

    /**
     * 查询所有后台分类
     * @return
     */
    @GetMapping("listAll")
    public CommonResult listAll(){
        List<UmsResourceCategory> list = umsResourceCategoryService.list();
        return CommonResult.success(list,"查询成功!");
    }

    /**
     * 添加后台资源分类
     * @param umsResourceCategory
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody UmsResourceCategory umsResourceCategory){
        Date date=new Date();
        umsResourceCategory.setCreateTime(date);
        umsResourceCategoryService.save(umsResourceCategory);
        return CommonResult.success("添加成功!");
    }

    /**
     * 根据ID删除后台资源
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id){
        umsResourceCategoryService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 修改后台资源分类
     * @param umsResourceCategory
     * @param id
     * @return
     */
    @PostMapping("update/{id}")
    public CommonResult update(@RequestBody UmsResourceCategory umsResourceCategory,@PathVariable Long id){
        umsResourceCategory.setId(id);
        Date date=new Date();
        umsResourceCategory.setCreateTime(date);
        umsResourceCategoryService.updateById(umsResourceCategory);
        return CommonResult.success("修改成功!");
    }
}
